package com.jiangsong.service;

import com.jiangsong.mq.MyMqSender;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionSynchronization;
import org.springframework.transaction.support.TransactionSynchronizationManager;

@Service
@Transactional
@Slf4j
public class MyService {

    @Autowired
    private MyMqSender mqProducerService;


    public void executeByOrdinary(String mesg) {
        log.info("test execute by ordinary begin, mesg:{}", mesg);
        mqProducerService.sendMsg("test消息内容为" + "新增" + mesg + "成功");

        log.info("test execute by ordinary end, mesg:{}", mesg);

    }

    public void execute(String mesg) {
        log.info("test execute begin");
        TransactionSynchronizationManager.registerSynchronization(new TransactionSynchronization() {
            @Override
            public void afterCommit() {
// 事务提交后执行的操作
                mqProducerService.sendMsg(mesg);
                System.out.println("test 事务提交后执行");
            }
        });
        log.info("test execute end");
    }


}
